package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.constant.RedisConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AddCountDeposit;
import com.tbit.uqbike.object.pojo.AddCountFee;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.AddCountDepositService;
import com.tbit.uqbike.service.business.AddCountFeeService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.AddCountFeeDao;
import com.tbit.uqbike.webmanager.dao.core.MachineFeeBoundDao;
import com.tbit.uqbike.webmanager.dao.core.RideCardDao;
import com.tbit.uqbike.webmanager.dao.core.VipCardDao;
import com.tbit.uqbike.webmanager.util.DateUtils;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenmengran
 * @Description: 计费规则
 * @date 2019\12\26 002617:03
 */
@Service
public class AddCountFeeServiceImpl implements AddCountFeeService {
	@Autowired
	private AddCountFeeDao addCountFeeDao;
	@Autowired
	private PurviewService purviewService;
	@Autowired
	private MachineService machineService;
	@Autowired
	private AddCountDepositService addCountDepositService;
	@Autowired
	private RedisService redisService;
	@Autowired
	private MachineFeeBoundDao machineFeeBoundDao;
	@Resource
	private RideCardDao rideCardDao;
	@Resource
	private VipCardDao vipCardDao;
	@Autowired
	private SensitivenessServiceImpl sensitivenessService;

	@Override
	public void add(LoginInfo loginInfo, AddCountFee addCountFee) {
		if (!purviewService.purviewCheck(loginInfo.id, addCountFee.getAccountId())) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}
		/* *//**获取该品牌的短信配置信息*//*
        Integer brandId = accountDao.getBrandIdByAccountId(addCountFee.getAccountId(), 3);
        SmsAuthConfig smsConfig = smsAuthConfigDao.getByAccountId(brandId);
        *//**站外短信开关判断是否配置了短信*//*
        if (!(addCountFee.getDispatchSwitch().equals(1)  && addCountFee.getAreaSmsSwitch().equals(1) && null != smsConfig && null != smsConfig.getAreaSmsModelId())) {
            throw new BaseException(SystemCodeEnum.SMSMODELID_NOT_DEPLOY);
        }
        *//**区域短信开关判断是否配置了短信*//*
        if (!(addCountFee.getDispatchSwitch().equals(1)  && addCountFee.getParkPointSmsSwitch().equals(1)  && null != smsConfig && null != smsConfig.getParkPointSmsModelId())) {
            throw new BaseException(SystemCodeEnum.SMSMODELID_NOT_DEPLOY);
        }*/
		addCountFeeDao.insert(addCountFee);
		/**清除redis数据*/
		redisService.del(RedisConstant.FEERULE_KEY_PREFIX + addCountFee.getAccountId());
	}

	@Override
	public int add(AddCountFee addCountFee) {
		if (StrUtil.isNotEmpty(addCountFee.getName()) && addCountFee.getName().length() > 50){
			throw new BaseException("计费名称过长");
		}
		if (null != addCountFeeDao.getInfo(addCountFee.getFeeId())) {
			sensitivenessService.sendMessage(addCountFee.getAccountId(), 1);
			return addCountFeeDao.updateCountFee(addCountFee);
		}
		addCountFee.setAddTime(DateUtils.getNowDate());
		return addCountFeeDao.add(addCountFee);
	}

	@Override
	public AddCountFee getFee(LoginInfo loginInfo, Integer accountId) {
		if (!purviewService.purviewCheck(loginInfo.id, accountId)) {
			throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
		}
		return addCountFeeDao.getByAccountId(accountId);
	}

	@Override
	public AddCountFee getByUserCode(String userCode) {
		Machine machine = machineService.getByUserCode(userCode);
		if (machine == null) {
			throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
		}
		AddCountDeposit addCountDeposit = addCountDepositService.getByAccountId(machine.getAccountId());
		AddCountFee addCountFee = addCountFeeDao.getByAccountId(machine.getAccountId());
		if (StringUtils.isNotNull(addCountDeposit)) {
			addCountFee.setDeposit(addCountDeposit.getDeposit());
		}
		return addCountFee;
	}

	@Override
	public List<AddCountFee> getMachineFeeList(AddCountFee addCountFee) {
		return addCountFeeDao.getMachineFeeList(addCountFee);
	}

	@Override
	public AddCountFee getInfo(Integer feeId) {
		return addCountFeeDao.getInfo(feeId);
	}

	@Override
	public int deleteRoleByIds(Integer[] feeIds) {
		/**这个feeIds只会有一个*/
		for (Integer feeId : feeIds) {
			Map<String, Object> map = new HashMap<>(2);
			map.put("feeId", feeId);
			Integer flag = rideCardDao.queryCount(map);
			if (flag > 0) {
				throw new BaseException("此计费规则绑定了骑行卡,请先解绑");
			}
			Integer vipFlag = vipCardDao.selectVipCardByFeeId(map);
			if (vipFlag > 0) {
				throw new BaseException("此计费规则绑定了会员卡,请先解绑");
			}
		}
		int row = addCountFeeDao.deleteRoleByIds(feeIds);
		machineFeeBoundDao.deleteByFeeId(feeIds);
		return row;
	}
}